﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace csharp窗口句柄z
{
    public class MouseHook
    {

        private Point point;

        private Point Point
        {

            get { return point; }

            set
            {

                if (point != value)
                {

                    point = value;

                    if (MouseMoveEvent != null)
                    {

                        var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);

                        MouseMoveEvent(this, e);

                    }

                }

            }

        }

        private int hHook;

        public const int WH_MOUSE_LL = 14;

        public Win32Api.HookProc hProc;

        public MouseHook() { this.Point = new Point(); }

        public int SetHook()
        {

            hProc = new Win32Api.HookProc(MouseHookProc);

            hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);

            return hHook;

        }

        public void UnHook()
        {

            Win32Api.UnhookWindowsHookEx(hHook);

        }

        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {

            Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct));

            if (nCode < 0)
            {

                return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);

            }

            else
            {

                this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);

                return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);

            }

        }

        //委托+事件（把钩到的消息封装为事件，由调用者处理）

        public delegate void MouseMoveHandler(object sender, MouseEventArgs e);

        public event MouseMoveHandler MouseMoveEvent;

    }
}
